package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.NursingPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import org.springframework.transaction.annotation.Transactional;


/**
 * 护理计划Service业务层处理
 * 
 * @author winnie
 * @date 2025-10-07
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //根据护理计划ID查询护理项目的关系
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }
    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto)
    {
        // 1、保存护理计划

        NursingPlan nursingPlan = new NursingPlan();

        // nursingPlan.setSortNo(dto.getSortNo());
        // nursingPlan.setPlanName(dto.getPlanName());
        // nursingPlan.setStatus(dto.getStatus());
        BeanUtils.copyProperties(dto, nursingPlan); // 属性名 数据类型要一致
        nursingPlan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        // 2、护理项目和护理计划中间表

        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();

        // 把dto转成实体类
        List<NursingProjectPlan> projectPlanList = projectPlans.stream().map(d -> {
            NursingProjectPlan projectPlan = new NursingProjectPlan();
            projectPlan.setPlanId(nursingPlan.getId());   //护理计划的id
            projectPlan.setProjectId((long) Integer.parseInt(d.getProjectId()));
            projectPlan.setExecuteTime(d.getExecuteTime());
            projectPlan.setCreateTime(new Date());
            projectPlan.setExecuteCycle(Long.valueOf(d.getExecuteCycle()));
            projectPlan.setExecuteFrequency(Long.valueOf(d.getExecuteFrequency()));
            return projectPlan;
        }).collect(Collectors.toList());



        nursingProjectPlanMapper.insertBacth(projectPlanList);



        return 1;
    }


    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlanMapper.updateNursingPlan(nursingPlan);

        //根据id删除中间表的数据
        nursingProjectPlanMapper.deleteNursingProjectPlanById(Long.valueOf(dto.getId()));

        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();

        if (projectPlans != null && projectPlans.size() > 0) {
            List<NursingProjectPlan> projectPlanList = projectPlans.stream().map(d -> {
                NursingProjectPlan projectPlan = new NursingProjectPlan();
                projectPlan.setPlanId(nursingPlan.getId());   //护理计划的id
                projectPlan.setProjectId((long) Integer.parseInt(d.getProjectId()));
                projectPlan.setExecuteCycle(Long.valueOf(d.getExecuteCycle()));
                projectPlan.setExecuteFrequency(Long.valueOf(d.getExecuteFrequency()));
                projectPlan.setCreateTime(new Date());
                projectPlan.setExecuteTime(d.getExecuteTime());
                return projectPlan;

            }).collect(Collectors.toList());
            nursingProjectPlanMapper.insertBatch(projectPlanList);
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        try {
            //删除关系
            //删除护理计划与护理项目的关系
            nursingProjectPlanMapper.deleteByPlanId(id);
            return nursingPlanMapper.deleteNursingPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
